﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using SdmxMl.Helper;

namespace SdmxMl.GenericData
{
    /// <summary>
    /// ValueType is used to assign a single value to a concept, as for attribute 
    /// values and key values. It has no element content. The startTime attribute 
    /// is only used if the textFormat of the attribute is of the Timespan type in 
    /// the key family (in which case the value field takes a duration).
    /// </summary>
    public class ValueType
    {
        #region Properties

        private string _conceptId;
        public string Concept
        {
            get { return _conceptId; }
            set { _conceptId = value; }
        }

        private string _value;
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        private string _startTime;
        public string StartTime
        {
            get { return _startTime; }
            set { _startTime = value; }
        }
        
        #endregion //Properties

        #region Construction

        public ValueType()
        {
            Init();
        }

        private void Init()
        {
        }

        public ValueType(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Init();
            _value = Xmlhelper.GetAtt(nodeArtefact, "value");
            _conceptId = Xmlhelper.GetAtt(nodeArtefact, "concept");
            _startTime = Xmlhelper.GetAtt(nodeArtefact, "startTime");
        }

        #endregion //Construction

        #region XML serialize

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);
            Xmlhelper.WriteAttributeIfExist(xw, "concept", _conceptId);
            Xmlhelper.WriteAttributeIfExist(xw, "value", Value);
            Xmlhelper.WriteAttributeIfExist(xw, "startTime", StartTime);
            xw.WriteEndElement();
        }

        #endregion //XML serialize

    }

    /// <summary> List of values </summary>
    public class ValueTypeList : List<ValueType>
    {
        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);
            foreach (ValueType v in this)
                v.WriteXml(xw, prefix, "Value");
            xw.WriteEndElement();
        }

    }
}
